//
//  SNOrderFormListDataManager.m
//  app
//
//  Created by 王少宁 on 15/5/5.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//  订单列表

#import "SNOrderFormListDataManager.h"

@interface SNOrderFormListDataManager ()
@property (nonatomic, retain) SNUniteUrl * uniteUrl;
@property (nonatomic, retain) JKHttpClient * http;
@property (nonatomic, retain) NSMutableArray * mutableArray;
@end

@implementation SNOrderFormListDataManager

+(SNOrderFormListDataManager *)shareManager
{
    AS_SINGLETON(SNOrderFormListDataManager, manager, {
        manager = [[SNOrderFormListDataManager alloc] init];
        manager.uniteUrl = [SNUniteUrl shareUniteUrl];
        manager.http = [[JKHttpClient alloc] init];
        manager.mutableArray = [[NSMutableArray alloc] init];
     })
    return manager;
}

- (void)loadOrderFormListData:(SNOrderFormListHttpResult *)result lastid:(int)lastid limit:(int)limit ostatus:(NSString *)ostatus
{
    if ([self.http isRunning]) {
        return;
    }
    result.failure = YES;
    result.success = NO;
    
    NSDictionary * params = @{@"lastid":[NSString stringWithFormat:@"%d",lastid],
                              @"limit":[NSString stringWithFormat:@"%d",limit],
                              @"ostatus":ostatus};
    
    DLog(@"%@   %@",self.uniteUrl.sbpd_orderFormListUrl,params);
    
    [self.http get:self.uniteUrl.sbpd_orderFormListUrl params:params result:^(JKResponse *resp) {

        DLog(@"%@",resp.content);

        if (resp.OK) {
            result.success = YES;
            result.failure = NO;
            if (resp.status == JKResponseOK) {
                if (lastid == 0) {
                    DLog(@"请求了第一页的数据");
                }
                NSArray * array = resp.content[@"result"];
                
                result.dataArray = [self orderFormListDataWithResult:result array:array lastid:lastid limit:limit];
                
            } else if (resp.status == JKResponseTail) {
                if (lastid == 0) {
                    result.noData = YES;
                } else {
                    result.noMoreData = YES;
                }
            }
        } else if (resp.failure) {
            result.failure = YES;
            result.success = NO;
        }
        [result invoke];
    }];
}

- (NSArray *)orderFormListDataWithResult:(SNOrderFormListHttpResult *)result array:(NSArray *)arry lastid:(int)lastid limit:(int)limit;
{
    if (lastid == 0) {
        [self.mutableArray removeAllObjects];
    }
    for (NSDictionary * dict in arry) {
        SNOrderFormListHeaderModel * model = [SNOrderFormListHeaderModel modelWithDict:dict];
        [self.mutableArray addObject:model];
    }
    
    if (!self.mutableArray.count) {
        result.noData = YES;
    } else {
        if (arry.count < limit) {
            result.noMoreData = YES;
        }
    }
    
    return self.mutableArray;
}


#pragma mark - 删除商品
- (void)deleteOrederFormList:(SNDeleteOrderHttpResult *)result sn:(NSString *)sn
{
    if ([self.http isRunning]) {
        return;
    }
    
    result.success = NO;
    result.failure = YES;
    
    NSDictionary * params = @{@"sn":sn,
                              @"display":@"1"};
    [self.http post:self.uniteUrl.sbpd_deleteOrderUrl params:params result:^(JKResponse *resp) {
        
        if (resp.status == 0) {
            result.success = YES;
            result.failure = NO;
        }
        
        [result invoke];
    }];
}


@end
